//
//  HJGroupListController.m
//  KaiChat
//
//  Created by lx on 2023/11/2.
//

#import "HJGroupListController.h"
#import "HJIM.h"
#import "HJGroupListCell.h"
#import "HJSectionHeaderView.h"
#import "HJChatController.h"

@interface HJGroupListController ()

@property (nonatomic, strong) NSArray *datas;

@end

@implementation HJGroupListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 60)];
    headerView.backgroundColor = UIColor.whiteColor;
    
    UIView *searchView = [UIView addToView:headerView];
    searchView.backgroundColor = UIColor.tj_F6F6F6Color;
    searchView.addCornerRadius(10);
    [searchView addTapGesturesWithTarget:self action:@selector(tapSearchView)];
    [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.top.offset(10);
        make.height.offset(40);
    }];
    
    UIImageView *searchIcon = [UIImageView addToView:searchView];
    searchIcon.image = [UIImage imageNamed:@"search_icon"];
    [searchIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.offset(15);
    }];
    
    UILabel *searchTextLabel = [UILabel lableWithSuperView:searchView font:UIFontMake(14) color:UIColor.tj_BEBDCAColor title:@"search" textAlignment:NSTextAlignmentLeft];
    [searchTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(searchIcon.mas_right).offset(10);
        make.centerY.offset(0);
    }];
    
    self.navigationBar.title = @"Groups";
    self.view.backgroundColor = UIColor.tj_F6F6F6Color;
    self.tableViewStyle = UITableViewStylePlain;
    self.tableView.tableHeaderView = headerView;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, TabbarSafeBottomMargin, 0);
    [self.tableView registerClass:HJGroupListCell.class forCellReuseIdentifier:@"HJGroupListCell"];
    [self.tableView registerClass:HJSectionHeaderView.class forHeaderFooterViewReuseIdentifier:@"HJSectionHeaderView"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 70;
    self.tableView.backgroundColor = UIColor.tj_F6F6F6Color;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight);
        make.left.right.bottom.offset(0);
    }];
    
    [HJIM.shareIM.groupManager loadGroupListWithCompletion:^(NSArray<NSArray<HJChatGroupModel *> *> * _Nullable datas) {
        self.datas = datas;
        [self.tableView reloadData];
    }];
}

- (void)tapSearchView {
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *datas = self.datas[section];
    return [datas count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJGroupListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJGroupListCell" forIndexPath:indexPath];
    HJChatGroupModel *model = self.datas[indexPath.section][indexPath.row];
    cell.model = model;
    return cell;
}
 
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
     NSArray *sectionDatas = self.datas[section];
    return sectionDatas.count == 0 ? CGFLOAT_MIN : 20;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    NSArray *sectionDatas = self.datas[section];
    HJSectionHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HJSectionHeaderView"];
    if (section == 0) {
        view.titleLabel.text = sectionDatas.count == 0 ? nil : [NSString stringWithFormat:@"Groups I created (%ld total)", sectionDatas.count];
    } else {
        view.titleLabel.text = sectionDatas.count == 0 ? nil : [NSString stringWithFormat:@"Groups I’ve joined (%ld toal)", sectionDatas.count];
    }
    view.contentView.backgroundColor = UIColor.tj_F6F6F6Color;
    view.backgroundColor = UIColor.tj_F6F6F6Color;
    view.titleLabel.textColor = UIColor.tj_BEBDCAColor;
    view.titleLabel.font = UIFontMake(12);
    return view;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColor.clearColor;
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HJChatGroupModel *groupModel = self.datas[indexPath.section][indexPath.row];

    HJChatController *chatController = [HJChatController chatControllerWithSessionModel:[HJChatSessionModel modelWithBlock:^(HJChatSessionModel * _Nonnull model) {
        model.sessionId = toString(groupModel.groupId);
        model.sessionName = groupModel.name;
        model.chatType = HJChatTypeGroup;
    }]];
    [self.navigationController pushViewController:chatController];
}

- (void)dealloc {
    NSLog(@"%s", __func__);
}

@end
